#include "multih_server.h"

#include <iostream>
#include <cstdlib>
#include <unistd.h>

static void printUsage(const char* argV0)
{
    TMultiHttpFileServer::TOptions  defaultOptions("");
    std::cout << argV0 << " usage:" << std::endl
              << "\t-h - show this message" << std::endl
              << "\t-c - unserved connection buffer size(" << defaultOptions.ConnectionQueueSize << ")" << std::endl
              << "\t-w - worker pool size(" << defaultOptions.PoolSize << ")" << std::endl
              << "\t-m - worker memory buffer size(" << defaultOptions.InternalBufferSize << ")" << std::endl
              << "\t-d - dir to serve (required)" << std::endl
              << "\t-n - listen buffer size (" << defaultOptions.NetworkQueueSize << ")" << std::endl
              << "\t-p - port number(" << defaultOptions.Port << ")" << std::endl;
}


int main(int argC, char** argV)
{
    TMultiHttpFileServer::TOptions  options("");
    char    c;
    while ((c = getopt(argC, argV, "hc:w:m:d:n:p:")) != -1) {
        switch(c) {
            case 'h':
                printUsage(argV[0]);
                return 0;

            case 'c':
                options.ConnectionQueueSize    =   atoi(optarg);
                break;

            case 'w':
                options.PoolSize    =   atoi(optarg);
                break;

            case 'm':
                options.InternalBufferSize  =   atoi(optarg);
                break;

            case 'd':
                options.Path        =   optarg;
                break;

            case 'n':
                options.NetworkQueueSize    =   atoi(optarg);
                break;

            case 'p':
                options.Port    =   optarg;
                break;

            default:
                printUsage(argV[0]);
                return 1;
        }
    }

    if (!options.IsValid()) {
        printUsage(argV[0]);
        return 1;
    }

    TMultiHttpFileServer    multih;
    multih.IoLoop(options);
    return 0;
}
